<?php

class CategoryController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 新建分类
	 */
	public function actionCreate()
	{
		$model=new Category;
		$model->zca_parentid = (int)$_GET['pid'];
		
		$parent = Category::model()->findByPk((int)$_GET['pid']);
		if(!empty($parent)){
			$model->zca_level = $parent->zca_level+1;
			$model->zca_topid = $parent->zca_parentid;
			
			$criteria=new CDbCriteria;
			$criteria->compare('zca_parentid',$parent->zca_id);
			$criteria->order = ' zca_listorder DESC ';
			$maxlistorder = Category::model()->find($criteria);
			$model->zca_listorder = $maxlistorder->zca_listorder+1;
		}else{
			$model->zca_level = 1;
			$model->zca_topid = 0;
		}
		//ajax增加行业分类
		if(isset($_GET['ajax'])){
			$model->zca_childid = 1;
			$model->zca_title = '分类名称';
			if($model->save()){
				echo $model->zca_id;exit;
			}else{
				echo "-1";exit;
			}
		}
		
		$model->zca_title = trim($_GET['title']);
		$model->zca_childid = 1;
		$model->save();
		$this->redirect($this->pre_module_url.'/category/admin');
	}

	/**
	 * 更新排序
	 */
	public function actionUpdate()
	{
		if(isset($_GET['ajax'])){
			$model = Category::model()->findByPk((int)$_GET['id']);
			$model->zca_title = array_iconv(trim($_GET['title']));
			if($model->save()){
				echo 1;exit;
			}else{
				echo "保存失败！";exit;
			}
		}
		if(isset($_POST['Category'])){
			$data = $_POST['Category'];
			$num = count($data['zca_id']);
			foreach( (array)$data['zca_id'] as $key=>$val){
				$model = Category::model()->findByPk($val);
				$model->zca_title = $data['zca_title'][$key];
			//	$model->zta_id = $data['zta_id'][$key];
				$model->zca_listorder = $key;
				$model->save();
			}
			$this->redirect($this->pre_module_url.'/category/admin');
		}else{
			throw new CHttpException(404,'The requested page does not exist.');
		}
	}

	/**
	 *删除分类
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id);
		
		//分类下有产产品不能删除
		$criteria=new CDbCriteria;
		$criteria->join = ' LEFT JOIN {{category}} zca ON zca.zca_id=t.zca_id ';
		$criteria->addCondition(" (zca.zca_id={$model->zca_id} OR zca.zca_parentid={$model->zca_id} OR zca.zca_topid={$model->zca_id})");
		
		//ajax删除
		if(isset($_GET['ajax'])){
			if(Product::model()->exists($criteria)){
				echo "分类下有产品不能删除";exit;
			}
			
			if($model->zca_childid==2){
				echo "分类下有子分类，不能删除";exit;
			}

			if($model->delete()){
				//如父级分类下无子分类，修改低分类下子分类的状态
				$criteria=new CDbCriteria;
				$criteria->compare('zca_parentid',$model->zca_parentid);
				if(!Category::model()->exists($criteria)){
					$parent = Category::model()->findByPk($model->zca_parentid);
					$parent->zca_childid = 1;
					$parent->zca_arrchildid = "";
					$parent->save();
				}
				echo 1;exit;
			}else{
				echo "删除失败，稍后再试！";exit;
			}
		}
		
		if(Product::model()->exists($criteria)){
			$this->render('../index/error',array('message'=>'对不起，分类下有产品不能删除！'));
			exit;
		}
		
		if($model->zca_childid==2){
			$this->render('../index/error',array('message'=>'对不起，分类下有子分类不能删除！'));
			exit;
		}
		if($model->delete()){
			//如父级分类下无子分类，修改低分类下子分类的状态
			$criteria=new CDbCriteria;
			$criteria->compare('zca_parentid',$model->zca_parentid);
			if(!Category::model()->exists($criteria)){
				$parent = Category::model()->findByPk($model->zca_parentid);
				$parent->zca_childid = 1;
				$parent->zca_arrchildid = "";
				$parent->save();
			}
			 $this->redirect(Yii::app()->request->urlReferrer);
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，分类信息修改失败，请稍后再试！'));
			exit;
		}
	}
	

	/**
	 * 编辑分类
	 */
	public function actionUpdateSeo($id){
		$model = $this->loadModel($id);
		
		if(isset($_POST['Category'])){
			$model->attributes = $_POST['Category'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，分类信息修改失败，请稍后再试！'));
				exit;
			}
		}
		
		$this->render('update_seo',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 分类管理.
	 */
	public function actionAdmin()
	{
		//更新分类子排序
		//Category::model()->acceptChild();

		$this->render('admin');
	}
	/**
	 * 载入模型
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Category::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	/**
	 * ajax数据验证
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='category-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	/**
	 * AJAX绑定模型
	 */
	public function actionAjaxBindAttMod(){
		if(isset($_GET['zca_id'])&&isset($_GET['zmd_id'])){
			$zca_id = (int)$_GET['zca_id'];
			$zmd_id = (int)$_GET['zmd_id'];
			if($zmd_id==0){
				echo "未选择模型！";exit;
			}
			$model = Category::model()->findByPk($zca_id);
			$model->zmd_id = $zmd_id;
			if(!$model->save()){
				"分类信息保存失败！";exit;
			}
			$attmod = AttMod::model()->findByPk($zmd_id);
			if(!empty($attmod->zmd_val)){
				$attval = unserialize($attmod->zmd_val);
				foreach($attval as $val){
					$attribute = new Attribute();
					$attribute->attributes = $val;
					$attribute->zca_id = $zca_id;
					$attribute->za_type = 1;
					$attribute->save();
				}
			}
			if(!empty($attmod->zmd_spe)){
				$attspe = unserialize($attmod->zmd_spe);
				foreach($attspe as $val){
					$attribute = new Attribute();
					$attribute->attributes = $val;
					$attribute->zca_id = $zca_id;
					$attribute->za_type = 2;
					$attribute->save();
				}
			}
			echo 1;exit;
		}else{
			echo "未选择模型！";exit;
		}
	}
	
}
